#include "EventDispatcher.h"

using namespace std;

EventDispatcher::EventDispatcher(void)
{
}

EventDispatcher::~EventDispatcher(void)
{
}

void EventDispatcher::addEventListaner(Event &e, FatBase<void> *method)
{
	EventsMap::iterator it = events.find(e.type);
	if(it != events.end())
		it->second.push_back( method );
	else
		events[e.type].push_back( method );
}

void EventDispatcher::removeEventListener(Event &e, FatBase<void> *method)
{
	EventsMap::iterator it = events.find(e.type);
	if(it != events.end())
		it->second.remove( method );
}

void EventDispatcher::dispatchEvent(Event *e)
{
	EventsMap::iterator it = events.find(e->type);
	if(it != events.end())
	{
		EventsList::iterator listIt = it->second.begin();
		while( listIt != it->second.end() )
		{
			(*(listIt++))->callMethod(e);//post decrementation to prevent listIt invalidation if removeEvent is called during callMethod
			//++listIt;
		}
	}
}